import { createRouter } from '@gowiny/uni-router'
import PAGE_DATA from '@/pages.json';
import { getToken } from '@/utils/auth.js'
import modal from '@/plugins/modal'

// const token = uni.getStorageSync('token')
const router = createRouter({
    pageData:PAGE_DATA
})
 const whiteList = ['/pages/login/index'];
router.beforeEach((to,from)=>{
   
  console.log("to.path", to.path)

  // 判断白名单
  if(whiteList.indexOf(to.path) == -1){
	   // 判断当前是否已经登录
		if(getToken()){
			// next();
			uni.navigateTo({
				url: to.path
			});
		}else{
			setTimeout(()=>{
				//拦截后一直跳不过去，加上定时器后就好了
				uni.navigateTo({
					url: '/pages/login/index'
				});
				modal.msg("请先登录！")
			
			},0)
		}
    }else{
		// 如果当前路径白名单，则放行
        uni.navigateTo({
            url: to.path
        });

    }
})

export default router
